package Day12;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author kalista
 * @Description
 * @Date 2020/11/18  15:48
 **/
public class Student {

    private String name;
    private int age;


    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void add(){

    }

    void eat(){

    }

    protected void sleep(){

    }

    private void work(){

    }


    public static void main(String[] args) {
        // class 三种实现

        // 第一种 方式获获得class对象
        Student student = new Student();
        Class aClass = student.getClass();
//        System.out.println(aClass.getSimpleName());

        // 第二种 获取class对象
       Class classs = Student.class;
//       System.out.println(classs == aClass);


       // 第三种 获取class对象            包名.类名
        try {
            Class classs2 =  Class.forName("Day12.Student");
            // 获取公有 无参构造
            Constructor[] constructors = classs2.getConstructors();
            for (Constructor constructor : constructors) {
                System.out.println(constructor.getName());
            }
            //公开字段
//            Field name = classs2.getField("name");
//            System.out.println(name);

            //私有的

            Method work = classs2.getDeclaredMethod("work");
            work.setAccessible(true);//解除私有限定
            //第一参数 调用对象 一个就是实参
            Object invoke = work.invoke("sdf");
            System.out.println(work);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }


}
